/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.persistence.base;

import org.proteusframework.core.api.IPlatformDelegate;
import org.proteusframework.core.util.Assert;
import org.proteusframework.platformservice.IExtendedPlatformDelegate;
import org.proteusframework.platformservice.persistence.api.IMessageBeanManager;
import org.proteusframework.platformservice.persistence.api.IPackageManager;
import org.proteusframework.platformservice.persistence.api.IProjectManager;
import org.proteusframework.platformservice.persistence.api.IVFSManager;

public abstract class AbstractPackageManager implements IPackageManager
{
    private IExtendedPlatformDelegate delegate = null;

    @Override
    public boolean supportsPackageManagement()
    {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final <T extends IPlatformDelegate> void setPlatformDelegate(final IPlatformDelegate delegate,
                                                                        Class<T> delegateInterface)
    {
        Assert.unitialized(this.delegate, "Application platform delegate is implicitly final and cannot be modified.");
        Assert.parameterNotNull(delegate, "Parameter 'delegate' must not be null");

        this.delegate = delegate.adapt(IExtendedPlatformDelegate.class);
    }

    protected IProjectManager getProjectManager()
    {
        return getDelegate().getPersistenceService().narrow(IProjectManager.class);
    }

    protected IVFSManager getVfsManager()
    {
        return getDelegate().getPersistenceService().narrow(IVFSManager.class);
    }

    protected IMessageBeanManager getMessageBeanManager()
    {
        return getDelegate().getPersistenceService().narrow(IMessageBeanManager.class);
    }

    protected IExtendedPlatformDelegate getDelegate()
    {
        return delegate;
    }
}
